package com.platform.modules.report.controller;


import com.platform.common.utils.DateUtils;
import com.platform.common.utils.MapUtil;
import com.platform.common.utils.NumberUtils;
import com.platform.modules.report.dao.PrjReportDao;
import com.platform.modules.report.dto.*;
import com.platform.modules.report.service.PrjReportService;
import com.platform.modules.sys.dao.SysCityAreaDao;
import com.platform.modules.sys.entity.SysCityAreaEntity;
import com.platform.modules.sys.entity.SysDictEntity;
import com.platform.modules.sys.service.SysDictService;
import com.platform.web.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 省级       province
 * 市级       city
 * 区县级     county
 * 各市       eachCity
 * 各区县     eachCounty
 * 年度区间   yearRange
 */
@SuppressWarnings("ALL")
@Api(tags = "报表 - 项目分析")
@RestController
@RequestMapping("report/prj")
public class PrjReportController extends BaseReportController {

    @Autowired
    PrjReportService prjReportService;
    @Autowired
    PrjReportDao prjReportDao;
    @Autowired
    SysDictService sysDictService;
    @Autowired
    SysCityAreaDao sysCityAreaDao;


    @ApiOperation("项目人数")
    @PostMapping("member/number")
    public CommonResult<Integer> memberNumber(@RequestBody BaseQueryDTO baseQueryDTO){
        return CommonResult.success(prjReportService.memberNumber(baseQueryDTO));
    }


    @ApiOperation("重点项目数：[importantPrjCount] 装配式建筑数：[zpsCount] 被动房建筑数：[bdfCount]")
    @PostMapping("yearPrjCount")
    public CommonResult<Map<String,Object>> yearPrjCount(@RequestBody BaseQueryDTO baseQueryDTO){

        Integer importantPrjCount = prjReportService.prjCount(baseQueryDTO , MapUtil.of("isImportant",1));
        Integer zpsCount = prjReportService.prjCount(baseQueryDTO , MapUtil.of("isZps",1));
        Integer bdfCount = prjReportService.prjCount(baseQueryDTO , MapUtil.of("isBdf",1));


        return CommonResult.success(MapUtil.of("importantPrjCount",importantPrjCount,"zpsCount",zpsCount,"bdfCount",bdfCount));
    }

    @ApiOperation("本月新增项目数【项目数 | 装配式 | 被动房】")
    @PostMapping("currMonthNewPrjCount")
    public CommonResult<Map<String,Object>> currMonthNewPrjCount(@RequestBody BaseQueryDTO baseQueryDTO){

        String month = DateUtils.getMonth().replaceAll("0","");

        Map extra = MapUtil.of("month", month);

        Integer prjCount = prjReportService.prjCount(baseQueryDTO , MapUtil.of("month", month));
        Integer zpsCount = prjReportService.prjCount(baseQueryDTO , MapUtil.of("month", month, "isZps",1));
        Integer bdfCount = prjReportService.prjCount(baseQueryDTO , MapUtil.of("month", month, "isBdf",1));


        return CommonResult.success(MapUtil.of("prjCount",prjCount,"zpsCount",zpsCount,"bdfCount",bdfCount));
    }

    @ApiOperation("工程规模分析 - 默认:饼图")
    @PostMapping("prjCountGroupByScale")
    public CommonResult<List<BaseItemDTO>> prjCountGroupByScale(@RequestBody BaseQueryDTO baseQueryDTO){
        Map<String,Object> map = prjReportService.prjCountGroupByScale(baseQueryDTO);
        List<SysDictEntity> dicts = sysDictService.queryByCode(MapUtil.of("code","PRJ_SCALE"));
        List<BaseItemDTO> data = this.buildDictItem(map,dicts);
        return CommonResult.success(data);
    }

    @ApiOperation("工程用途分析 - 默认:饼图")
    @PostMapping("purpose")
    public CommonResult<List<BaseItemDTO>> purpose(@RequestBody BaseQueryDTO baseQueryDTO){
        Map<String,Object> map  = prjReportService.prjCountGroupByPurpose(baseQueryDTO);
        List<SysDictEntity> dicts = sysDictService.queryByCode(MapUtil.of("code","PRJ_FUNCTION"));
        List<BaseItemDTO> data = this.buildDictItem(map,dicts);
        return CommonResult.success(data);
    }


    /**
     * 排名、市级地区、重点项目个数、装配式建筑个数、被动房建筑个数
     * @param dto
     * @return
     */
    @ApiOperation("各市项目分布 - 表格")
    @PostMapping("distribute")
    public CommonResult<List<DistributeDTO>> distribute(@RequestBody BaseQueryDTO baseQueryDTO){

        Map<String,Object> prjCountMap = prjReportService.importantPrjCountGroupByCity(baseQueryDTO);
        Map<String,Object> zpsCountMap = prjReportService.zpsCountGroupByCity(baseQueryDTO);
        Map<String,Object> bdfCountMap = prjReportService.bdfCountGroupByCity(baseQueryDTO);

        List<SysCityAreaEntity> areas = this.getAreas(baseQueryDTO.getProvinceNum());

        List<DistributeDTO> data = new ArrayList<>(areas.size());
        areas.forEach(e->{
            String xzqh = e.getId();
            Integer prjCount = NumberUtils.intValueOf0(prjCountMap.get(xzqh));
            Integer zpsCount = NumberUtils.intValueOf0(zpsCountMap.get(xzqh));
            Integer bdfCount = NumberUtils.intValueOf0(bdfCountMap.get(xzqh));
            data.add(new DistributeDTO(xzqh,e.getName(),prjCount,zpsCount,bdfCount));
        });

        return CommonResult.success(data);
    }

    @ApiOperation("省级 - 装配式Z、被动房B 建筑走势trend - 折线图")
    @PostMapping("zbAnnualTrend")
    public CommonResult zbAnnualTrend(@RequestBody BaseQueryDTO baseQueryDTO){

        List<String> yearList = baseQueryDTO.getYearList();
        //YEAR COUNT
        Map zpsCountMap = prjReportService.zpsCountGroupByYear(baseQueryDTO);
        Map bdfCountMap = prjReportService.bdfCountGroupByYear(baseQueryDTO);


//        List<BaseItemDTO> zpsData = new ArrayList<>(yearList.size());
//        List<BaseItemDTO> bdfData = new ArrayList<>(yearList.size());
//        yearList.forEach(year ->{
//            zpsData.add(new BaseItemDTO(year,zpsCountMap.getOrDefault(year,0),year));
//            bdfData.add(new BaseItemDTO(year,zpsCountMap.getOrDefault(year,0),year));
//        });

        List<BaseItemDTO> zpsData = this.buildStringItem(zpsCountMap,yearList);
        List<BaseItemDTO> bdfData = this.buildStringItem(bdfCountMap,yearList);

        List<BrokenLineDTO> data = Arrays.asList(
                new BrokenLineDTO("装配式建筑",zpsData),
                new BrokenLineDTO("被动房建筑",bdfData)
        );
        return CommonResult.success(data);
    }


    @ApiOperation("年度面积 - 柱图")
    @PostMapping("totalAreaGroupByYear")
    public CommonResult<List<BaseItemDTO>> totalAreaGroupByYear(@RequestBody BaseQueryDTO baseQueryDTO){
        Map<String,Object> map = prjReportService.prjTotalAreaGroupByYear(baseQueryDTO);
        List<BaseItemDTO> data = this.buildStringItem(map, baseQueryDTO.getYearList());
        return CommonResult.success(data);
    }


    @ApiOperation("年度项目数 - 柱图")
    @PostMapping("prjCountGroupByYear")
    public CommonResult<List<BaseItemDTO>> prjCountGroupByYear(@RequestBody BaseQueryDTO baseQueryDTO){
        Map<String,Object> map = prjReportService.prjCountGroupByYear(baseQueryDTO);
        List<BaseItemDTO> data = this.buildStringItem(map, baseQueryDTO.getYearList());
        return CommonResult.success(data);
    }

    @ApiOperation("年度金额数 - 柱图")
    @PostMapping("prjTotalInvestmentGroupByYear")
    public CommonResult<List<BaseItemDTO>> prjTotalInvestmentGroupByYear(@RequestBody BaseQueryDTO baseQueryDTO){
        Map<String,Object> map = prjReportService.prjTotalInvestmentGroupByYear(baseQueryDTO);
        List<BaseItemDTO> data = this.buildStringItem(map, baseQueryDTO.getYearList());
        return CommonResult.success(data);
    }


    @ApiOperation("各市面积 - 饼图")
    @PostMapping("totalAreaGroupByCity")
    public CommonResult<List<BaseItemDTO>> totalAreaGroupByCity(@RequestBody BaseQueryDTO baseQueryDTO){
        Map<String,Object> map = prjReportService.prjTotalAreaGroupByCity(baseQueryDTO);
        List<SysCityAreaEntity> areas = this.getAreas(baseQueryDTO.getProvinceNum());
        List<BaseItemDTO> data = this.buildAreaItem(map, areas);
        return CommonResult.success(data);
    }


    @ApiOperation("各市项目数 - 饼图")
    @PostMapping("prjCountGroupByCity")
    public CommonResult<List<BaseItemDTO>> prjCountGroupByCity(@RequestBody BaseQueryDTO baseQueryDTO){
        Map<String,Object> map = prjReportService.prjCountGroupByCity(baseQueryDTO);
        List<SysCityAreaEntity> areas = this.getAreas(baseQueryDTO.getProvinceNum());
        List<BaseItemDTO> data = this.buildAreaItem(map, areas);
        return CommonResult.success(data);
    }

    @ApiOperation("各市金额数 - 饼图")
    @PostMapping("prjTotalInvestmentGroupByCity")
    public CommonResult<List<BaseItemDTO>> prjTotalInvestmentGroupByCity(@RequestBody BaseQueryDTO baseQueryDTO){
        Map<String,Object> map = prjReportService.prjTotalInvestmentGroupByCity(baseQueryDTO);
        List<SysCityAreaEntity> areas = this.getAreas(baseQueryDTO.getProvinceNum());
        List<BaseItemDTO> data = this.buildAreaItem(map, areas);
        return CommonResult.success(data);
    }


}
